﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.CBO.SCM.Item;
    using UFIDA.U9.ISV.MO;
    using UFIDA.U9.ISV.MO.Proxy;
    using UFIDA.U9.MO.Issue;
    using UFIDA.U9.MO.MO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;
    using IssueDTOData = UFIDA.U9.ISV.MO.IssueDTOData;

    /// <summary>
    /// IssueSaveSV partial 
    /// </summary>	
    public partial class IssueSaveSV 
	{	
		internal BaseStrategy Select()
		{
			return new IssueSaveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class IssueSaveSVImpementStrategy : BaseStrategy
	{
		public IssueSaveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			IssueSaveSV bpObj = (IssueSaveSV)obj;

			try
			{
				if (bpObj.Data == null || bpObj.Data.Count == 0)
					throw new Exception("传入参数为空");
				List<IssueKeyDTOData> retlist = new List<IssueKeyDTOData>();

				//List<object> inputdtos = DTOUtils.DTOListPropCopy(bpObj.Data.ConvertAll(item => item as object)
				//	, typeof(IssueDTOData));
				foreach (var dto in bpObj.Data)
				{
					IssueDoc item = IssueDoc.Finder.Find($"DocNo='{dto.DocNo}'");
					if (item == null)
					{
						//新增逻辑
						preSave(dto);
						IssueDTOData dto1=new IssueDTOData();
						DTOUtils.DTOPropCopy(dto, dto1);
						CreateIssue4ExternalProxy proxy = new CreateIssue4ExternalProxy();
						proxy.IssueDTOs = new List<IssueDTOData>() { dto1 };
                        List<IssueKeyDTOData> ret = proxy.Do();
						if (ret.Count > 0)
							retlist.Add(ret[0]);

						//修改单号
						ret[0].DocNo=EntityUtils.UpdateDocNo<IssueDoc>(ret[0].ID, dto.DocNo);

						//自动审核
						IssueApprove approve = new IssueApprove();
						approve.Data = new List<CopyOfApproveIssueDoc4ExternalDTOData>
						{
							new CopyOfApproveIssueDoc4ExternalDTOData()
							{
								DocNo=ret[0].DocNo,
								OperateType=true
							}
						};
                        ResultDTOData resultDTOData = approve.Do();
						ResultUtils.CheckResult(resultDTOData);
					}
					else
					{
						//修改逻辑
						throw new AITException(599,$"单号:{dto.DocNo}已经存在,不能生成!");
					}
				}
				return ResultUtils.success(bpObj, bpObj.Data, retlist);
			}
			catch (Exception ex)
			{
				return ResultUtils.error(bpObj, bpObj.Data, ex);
			}
		}

		//保存前设置默认值
        private void preSave(CopyOfIssueDTOData dto)
        {
			//校验
			if (dto.DocNo == null || dto.DocNo.Length == 0)
				throw new Exception("传入单号[DocNo]为空");
			if (dto.DocType == null)
				throw new Exception("传入单据类型[DocType]为空");
			foreach(var line in dto.PickListDTOs)
            {
				if (line.Item == null)
					throw new Exception($"料品[Item]为空");
            }

			//赋值
			//业务类型
            IssueDocType doctype = EntityUtils.GetEntityByCode<IssueDocType>(dto.DocType.Code);
            dto.BusinessType = doctype.BusinessType.Value;

			//领料行
            foreach (var line in dto.PickListDTOs)
            {
				//设置领料单位
				ItemMaster item = ItemMaster.Finder.Find($"Code='{line.Item.Code}'");
				if (item == null)
					throw new Exception($"查找物料{line.Item.Code}失败!");
				line.IssueUOM = new CopyOfCommonArchiveDataDTOData
				{
					Code = item.MaterialOutUOM.Code
				};

                //查找备料
                MOPickList pick = MOPickList.Finder.Find($"MO.DocNo='{line.MODocNo}' and DocLineNO='{line.PickLineNo}'");
				if (pick == null) throw new Exception($"备料查询失败,单号:[{line.MODocNo}],行号:[{line.PickLineNo}]");
				//设置备料ID
				line.MOPickList = pick.ID;
				//校验
				if (pick.ItemMaster.Code != item.Code)
					throw new Exception($"来源料号[{pick.ItemMaster.Code}]与传入料号[{item.Code}]不同!");

				//数量设置
				line.IssuedQty = line.IssueQty;
            }
        }
    }

	#endregion
	
	
}